using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace BassMati.Client
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem menuConnect;
		private System.Windows.Forms.MenuItem menuDisconnect;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Button buttonSend;
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			InitializeComponent();
			Trace.IndentSize = 2;
		}

		
		protected System.Diagnostics.TraceSwitch traceLevel = new TraceSwitch( "MainFormTraceLevel", "Controls trace information output level." );
		BassMati.Client.Client client = new Client ( "127.0.0.1", 8080 );

		#region - Generated Code -
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.menuConnect = new System.Windows.Forms.MenuItem();
			this.menuDisconnect = new System.Windows.Forms.MenuItem();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.buttonSend = new System.Windows.Forms.Button();
			this.panel2 = new System.Windows.Forms.Panel();
			this.label3 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.panel1.SuspendLayout();
			this.panel2.SuspendLayout();
			this.SuspendLayout();
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuConnect,
																					 this.menuDisconnect});
			// 
			// menuConnect
			// 
			this.menuConnect.Index = 0;
			this.menuConnect.Text = "&Connect";
			this.menuConnect.Click += new System.EventHandler(this.menuConnect_Click);
			// 
			// menuDisconnect
			// 
			this.menuDisconnect.Index = 1;
			this.menuDisconnect.Text = "&Disconnect";
			this.menuDisconnect.Click += new System.EventHandler(this.menuDisconnect_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 8);
			this.label2.Name = "label2";
			this.label2.TabIndex = 0;
			this.label2.Text = "Send:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textBox2
			// 
			this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.textBox2.Location = new System.Drawing.Point(8, 32);
			this.textBox2.Multiline = true;
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(274, 48);
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "";
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.buttonSend);
			this.panel1.Controls.Add(this.label2);
			this.panel1.Controls.Add(this.textBox2);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(288, 120);
			this.panel1.TabIndex = 0;
			// 
			// buttonSend
			// 
			this.buttonSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.buttonSend.Location = new System.Drawing.Point(208, 88);
			this.buttonSend.Name = "buttonSend";
			this.buttonSend.TabIndex = 2;
			this.buttonSend.Text = "&Send";
			this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.label3);
			this.panel2.Controls.Add(this.textBox3);
			this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel2.Location = new System.Drawing.Point(0, 123);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(288, 150);
			this.panel2.TabIndex = 1;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 8);
			this.label3.Name = "label3";
			this.label3.TabIndex = 0;
			this.label3.Text = "Received:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textBox3
			// 
			this.textBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.textBox3.Location = new System.Drawing.Point(8, 32);
			this.textBox3.Multiline = true;
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(274, 112);
			this.textBox3.TabIndex = 1;
			this.textBox3.Text = "";
			// 
			// splitter1
			// 
			this.splitter1.BackColor = System.Drawing.SystemColors.ControlDark;
			this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
			this.splitter1.Location = new System.Drawing.Point(0, 120);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(288, 3);
			this.splitter1.TabIndex = 1;
			this.splitter1.TabStop = false;
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(288, 273);
			this.Controls.Add(this.panel2);
			this.Controls.Add(this.splitter1);
			this.Controls.Add(this.panel1);
			this.Menu = this.mainMenu;
			this.Name = "MainForm";
			this.Text = "Client";
			this.panel1.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		#endregion - Generated Code -

		private void menuConnect_Click(object sender, System.EventArgs e)
		{
			Trace.WriteLineIf ( traceLevel.TraceInfo, "MainForm.menuConnect_Click enter" );
			Trace.Indent ( );
			this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
			try
			{
				this.client.Connect ( );
				if ( this.client.Connected )  this.Text = string.Format( "Client - connected to {0}:{1}", this.client.HostName, this.client.PortNumber );
			}
			catch ( System.Exception ex )
			{
				MessageBox.Show( this, ex.Message, "Error (Client.exe)", MessageBoxButtons.OK, MessageBoxIcon.Error );
			}
			finally
			{
				Trace.Unindent ( );
				Trace.WriteLineIf ( traceLevel.TraceInfo, "MainForm.menuConnect_Click exit" );
				this.Cursor = System.Windows.Forms.Cursors.Default;
			}
		}

		private void menuDisconnect_Click(object sender, System.EventArgs e)
		{
			Trace.WriteLineIf ( traceLevel.TraceInfo, "MainForm.menuDisconnect_Click enter" );
			Trace.Indent ( );
			try
			{
				this.client.Disconnect ( );
				this.Text = "Client - disconnected";
			}
			catch ( System.Exception ex )
			{
				MessageBox.Show( this, ex.Message, "Error (Client.exe)", MessageBoxButtons.OK, MessageBoxIcon.Error );
			}
			finally
			{
				Trace.Unindent ( );
				Trace.WriteLineIf ( traceLevel.TraceInfo, "MainForm.menuDisconnect_Click exit" );
			}
		}

		private void buttonSend_Click(object sender, System.EventArgs e)
		{
			Trace.WriteLineIf ( traceLevel.TraceInfo, "MainForm.buttonSend_Click enter" );
			Trace.Indent ( );
			try 
			{
				this.client.Send ( this.textBox2.Text );
				this.textBox3.Text += "\r\n" + this.client.Receive ( );
			}
			catch ( Exception ex )
			{
				MessageBox.Show( this, ex.Message, "Error (Client.exe)", MessageBoxButtons.OK, MessageBoxIcon.Error );
			}
			finally 
			{
				Trace.Unindent ( );
				Trace.WriteLineIf ( traceLevel.TraceInfo, "MainForm.buttonSend_Click exit" );
			}
		}
	}
}
